<?php

namespace common\logics;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use yii\data\ActiveDataProvider;

/**
 * 商城栏目分类
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class ShopCategoryLogic extends \common\models\shop\ShopCategory {

    /**
     * @inheritdoc
     */
    public function search() {
        return static::findByParentId()->all();
    }

    /**
     * @inheritdoc
     */
    public static function recommend() {
        $data = static::findByParentId()->with('children')->limit(8)->all();
        $last = ArrayHelper::remove($data, 7);
        if ($last) {
            $all = [
                'id' => 0,
                'name' => '全部分类',
                'thumb' => Url::attach('/assets/icons/cateall.png'),
                'children' => []
            ];
            ArrayHelper::setValue($data, 7, $all);
        }
        return $data;
    }

    /**
     * @inheritdoc
     */
    public static function findByParentId($id = 0) {
        return static::find()
        ->active(static::STATUS_ACTIVE)
        ->andWhere(['parent_id' => $id])
        ->orderBy(['listorder' => SORT_ASC]);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'thumb' => function() {
                return Url::attach($this->icon);
            },
            'children'
        ];
    }

}
